//
//  XHMainViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-5-22.
//  Copyright (c) 2014年 broadwave.com. All rights reserved.
//

#import "XHMainViewController.h"
#import "POP.h"
#import "ReactiveCocoa.h"
#import "XHLeftViewController.h"
#import "XHRightViewController.h"
#import "XHHomeViewController.h"
#import "CustomIntroductionPanel.h"

@interface XHMainViewController ()
@property (nonatomic ,strong) UIView *leftView;  // 左边列表
@property (nonatomic ,strong) UIView *rightView; // 右边天气
@property (nonatomic ,strong) UIView *mainView;  // 展示视图
@property (nonatomic ,strong) UINavigationController *currentViewController;
@property (nonatomic, strong) XHLeftViewController *leftViewController;
@property (nonatomic, strong) XHRightViewController *rightViewContoller;
@property (nonatomic ,strong) UITapGestureRecognizer *tapGestureRecognizer; //
//@property (nonatomic ,strong) NSDictionary *controllerDictionary;
@end

@implementation XHMainViewController



// 单例
+ (id)shareXMMainViewController
{
    static dispatch_once_t once = 0;
    __strong static id sharedInstance = nil;
    dispatch_once(&once, ^{
        sharedInstance = [[XHMainViewController alloc] init];
    });
    return sharedInstance;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        CGRect rect = self.view.frame;
        self.view.backgroundColor = [UIColor colorWithRed:71.0/255 green:153.0/255 blue:211.0/255 alpha:1.0];
        _leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0.0, 270.0, rect.size.height)];
        _leftView.alpha = 0.0;
        _leftView.clipsToBounds = YES;
        
        _leftViewController = [[XHLeftViewController alloc] initWithNibName:@"XHLeftViewController" bundle:nil];
        [self addChildViewController:_leftViewController];
        _leftViewController.delegate = self;
        
        _leftViewController.view.frame= _leftView.bounds;
        [_leftView addSubview:_leftViewController.view];
        
        _rightView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-270.0, 0.0, 270.0, rect.size.height)];
        _rightView.alpha = 0.0;
        _rightView.clipsToBounds = YES;
        
        _rightViewContoller = [[XHRightViewController alloc] initWithNibName:@"XHRightViewController" bundle:nil];
        _rightViewContoller.view.frame = _rightView.bounds;
        [_rightView addSubview:_rightViewContoller.view];
        
        
        
        _mainView = [[UIView alloc] initWithFrame:rect];
        _mainView.backgroundColor = [UIColor clearColor];
        
        [self changeContentViewWithViewController:nil];
        
        [self.view addSubview:_leftView];
        [self.view addSubview:_rightView];
        [self.view addSubview:_mainView];
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addGestureRecognizer];
   
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    //lileltp 判断第一次启动
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
        [self displayIntroduceView];
    }
    
    

}

#pragma mark private method

// 添加手势，左滑和右滑

- (void)addGestureRecognizer
{
    UISwipeGestureRecognizer *swipeGestureRecognizer;
    swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeMainView:)];
   
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeGestureRecognizer];
    
    swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeMainView:)];
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeGestureRecognizer];
       _tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMainView:)];
    _tapGestureRecognizer.numberOfTapsRequired =1;
    _tapGestureRecognizer.numberOfTouchesRequired = 1;

}

//

- (void)swipeMainView:(id)sender
{
    
    UISwipeGestureRecognizer *swipeGestureRecognizer = (UISwipeGestureRecognizer *)sender;
    switch (swipeGestureRecognizer.direction) {
        case UISwipeGestureRecognizerDirectionLeft:
            [self animateSubViewsWithDirection:leftDirection];
            break;
        case UISwipeGestureRecognizerDirectionRight:
            [self animateSubViewsWithDirection:rightDirection];
            break;
        default:
            break;
    }
   
}

//

- (void)tapMainView:(id)sender
{
   
    
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    anim.toValue = [NSValue valueWithCGRect:self.view.frame];
    [_mainView pop_addAnimation:anim forKey:@"slide"];
    
    for (UIView *view in [_mainView subviews]) {
        view.userInteractionEnabled = YES;
    }
    [_mainView removeGestureRecognizer:_tapGestureRecognizer];
    

    POPBasicAnimation *alpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    alpha.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    alpha.fromValue = @(1.0);
    alpha.toValue = @(0.0);
    if (_mainView.frame.origin.x > 0) {
        [_leftView pop_addAnimation:alpha forKey:@"alpha"];

    }else{
    
        [_rightView pop_addAnimation:alpha forKey:@"alpha"];

    }
    
    
    
    

}

//

- (void)animateSubViewsWithDirection:(Direction)direction
{
    if (leftDirection == direction) {
        if (_mainView.frame.origin.x <0) {
            return;
        }
        
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        
        POPBasicAnimation *alpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
        alpha.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        if (_mainView.frame.origin.x == 0.0) {
            anim.toValue = [NSValue valueWithCGRect:CGRectMake(-260.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
            
            alpha.fromValue = @(0.0);
            alpha.toValue = @(1.0);
            [_rightView pop_addAnimation:alpha forKey:@"alpha"];
            [_mainView pop_addAnimation:anim forKey:@"slide"];
            for (UIView *view in [_mainView subviews]) {
                view.userInteractionEnabled = NO;
            }
            if (_mainView.userInteractionEnabled) {
               
               
                [_mainView addGestureRecognizer:_tapGestureRecognizer];
            }
            return;
        }
        
        if (_mainView.frame.origin.x > 0.0) {
            anim.toValue = [NSValue valueWithCGRect:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
            
            alpha.fromValue = @(1.0);
            alpha.toValue = @(0.0);
            [_leftView pop_addAnimation:alpha forKey:@"alpha"];
            [_mainView pop_addAnimation:anim forKey:@"slide"];
            for (UIView *view in [_mainView subviews]) {
                view.userInteractionEnabled = YES;
            }
            [_mainView removeGestureRecognizer:_tapGestureRecognizer];

            return;

        }
        
        return;
    }
    
    if (rightDirection ==direction) {
        
        if (_mainView.frame.origin.x >0.0) {
            return;
        }
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        POPBasicAnimation *alpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];

        alpha.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        if (_mainView.frame.origin.x == 0.0) {
            anim.toValue = [NSValue valueWithCGRect:CGRectMake(260.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
            alpha.fromValue = @(0.0);
            alpha.toValue = @(1.0);
            
            [_leftView pop_addAnimation:alpha forKey:@"alpha"];
            [_mainView pop_addAnimation:anim forKey:@"slide"];
            for (UIView *view in [_mainView subviews]) {
                view.userInteractionEnabled = NO;
            }
            
            [_mainView addGestureRecognizer:_tapGestureRecognizer];

            return;
            
        }
        
        if (_mainView.frame.origin.x < 0.0) {
            anim.toValue = [NSValue valueWithCGRect:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
           
            alpha.fromValue = @(1.0);
            alpha.toValue = @(0.0);
            [_rightView pop_addAnimation:alpha forKey:@"alpha"];
            [_mainView pop_addAnimation:anim forKey:@"slide"];
            for (UIView *view in [_mainView subviews]) {
                view.userInteractionEnabled = YES;
            }

            [_mainView removeGestureRecognizer:_tapGestureRecognizer];
            return;
        }
        return;
    }
}
#pragma mark 显示左右栏

- (void)showLeftView
{
    if (_mainView.frame.origin.x==0.0) {
        [self animateSubViewsWithDirection:rightDirection];
        
    }else{
    
        [self animateSubViewsWithDirection:leftDirection];
    }
}

- (void)showRightView
{
    if (_mainView.frame.origin.x ==0.0) {
        [self animateSubViewsWithDirection:leftDirection];
    }else{
    
        [self animateSubViewsWithDirection:rightDirection];
    }
}

- (void)changeContentViewWithViewController:(id)viewController
{
    
    
    if ( !_currentViewController) {
        XHHomeViewController *home = [[XHHomeViewController alloc] initWithNibName:@"XHHomeViewController" bundle:nil
                                      ];
        home.delegate = self;
        
        _currentViewController = [[UINavigationController alloc] initWithRootViewController:home];
        _currentViewController.navigationBarHidden = YES;
        [self addChildViewController:_currentViewController];
    
        [_mainView addSubview:_currentViewController.view];
    
        for (UIView *view in [_mainView subviews]) {
            view.userInteractionEnabled = YES;
        }
        [self tapMainView:nil];
        return;
    
    }else{
        NSAssert([viewController isKindOfClass:[XHBasicViewController class]], @"必须是XHBasicViewController ");

    }
    

    if ([_currentViewController isEqual:viewController]) {
        [self tapMainView:nil];
        
    }else{
        XHBasicViewController *basicViewController = (XHBasicViewController *)viewController;
        basicViewController.delegate = self;
        [_currentViewController.view removeFromSuperview];
        [_currentViewController removeFromParentViewController];
        [_currentViewController popToRootViewControllerAnimated:NO];
        
        
         _currentViewController = [[UINavigationController alloc] initWithRootViewController:basicViewController];
         _currentViewController.navigationBarHidden = YES;
        [self addChildViewController: _currentViewController];
        [_mainView addSubview: _currentViewController.view];
        
        for (UIView *view in [_mainView subviews]) {
            view.userInteractionEnabled = YES;
        }
        
        [self tapMainView:nil];

    }
    
    
    
    
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark 引导页

- (void)displayIntroduceView
{
    NSLog(@"333");
    CGRect frame = [[UIScreen mainScreen] bounds];
    CustomIntroductionPanel *panel1 = [[CustomIntroductionPanel alloc] initWithFrame:frame title:@"山西故事  全球视野    新华社与您分享" description:@"" image:[UIImage imageNamed:@"guide_1.png"]];
    
    CustomIntroductionPanel *panel2 = [[CustomIntroductionPanel alloc] initWithFrame:frame title:@"三晋之源 唐尧故地 晋源在您掌握" description:@"" image:[UIImage imageNamed:@"guide_2.png"]];
    CustomIntroductionPanel *panel3 = [[CustomIntroductionPanel alloc] initWithFrame:frame title:@"干群互联 你我互通 党政客户端" description:@"" image:[UIImage imageNamed:@"guide_3.png"]];
    
    NSArray *panels = @[panel1, panel2, panel3];
    
    MYBlurIntroductionView *introductionView = [[MYBlurIntroductionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    introductionView.delegate = self;
    introductionView.BackgroundImageView.image = [UIImage imageNamed:@"introduce_1.png"];
    introductionView.RightSkipButton.titleLabel.text = @"跳过";
    
    //    introductionView.LanguageDirection = MYLanguageDirectionRightToLeft;
    [introductionView buildIntroductionWithPanels:panels];
    introductionView.backgroundColor = [UIColor colorWithRed:13.0/255 green:133.0/255 blue:176.0/255 alpha:1.5];
    [self.view addSubview:introductionView];
    
}

- (void)introduction:(MYBlurIntroductionView *)introductionView didChangeToPanel:(CustomIntroductionPanel *)panel withIndex:(NSInteger)panelIndex
{
    if (panelIndex ==0) {
        //introductionView.backgroundColor = [UIColor colorWithRed:13.0/255 green:133.0/255 blue:176.0/255 alpha:0.5];
        
    }
    if (panelIndex ==1) {
        //introductionView.backgroundColor = [UIColor colorWithRed:214.0/255 green:78.0/255 blue:78.0/255 alpha:0.5];
    }
    if (panelIndex ==2) {
        
        [introductionView.RightSkipButton setFrame:CGRectMake(20, self.view.frame.size.height-55, 280, 30)];
        [introductionView.RightSkipButton.titleLabel removeFromSuperview];
        // introductionView.RightSkipButton.titleLabel.hidden = YES;
        // introductionView.backgroundColor = [UIColor colorWithRed:153.0/255 green:13.0/255 blue:22.0/255 alpha:0.8];
    }
}

- (void)introduction:(MYBlurIntroductionView *)introductionView didFinishWithType:(MYFinishType)finishType
{
    [introductionView removeFromSuperview];
    
}
@end
